package com.example.stmall.manager.mapper;

import com.example.stmall.manager.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 商品的Mapper层接口
 */
@Mapper
public interface GoodsMapper {

    /**
     * 插入数据表操作
     * 1.返回值类型 int
     * 2.方法名 insert
     * 3.参数 goods
     */
    @Insert({"insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})"})
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")   //获得自增主键
    int insert(Goods goods);

    /**
     * 根据ID删除
     */
    @Delete("delete from goods where id =#{id}")
    int delete(Integer id);

    /**
     *根据id修改，使用动态SQL
     */
    @Update({"<script>",
            "update goods",
            "<set>",
            "<if test= 'name!= null and name.length>0'>name=#{name},</if>",
            "<if test= 'dscp!= null and dscp.length>0'>dscp=#{dscp},</if>",
            "<if test= 'price!= null'>prcie=#{price},</if>",
            //设置数据表的mark_price字段 为 参数goods的markPrice属性
            "<if test= 'markPrice!= null'>mark_prcie=#{markPrice},</if>",
            "<if test= 'color!= null and color.length>0'>color=#{color},</if>",
            "<if test= 'version!= null and version.length>0'>version=#{version},</if>",
            "<if test= 'count!= null'>count=#{count},</if>",
            "<if test= 'content!= null and content.length>0'>content=#{content},</if>",
            "<if test= 'recom!= null'>recom=#{recom},</if>",
            //设置数据表的category_id字段 为 参数goods的categoryId属性
            "<if test= 'categoryId!= null'>category_id=#{categoryId},</if>",
            "<if test= 'status!= null'>status=#{status},</if>",
            "</set>",
            "where id =#{id}",
            "</script>"
    })
    int update(Goods goods);

    /**
     * 根据ID查询,图片也会查询
     */
    @Select("select * from goods where id =#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "id", property = "pics",
                    many = @Many(select = "com.example.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    Goods selectById(Integer id);

    /**
     * 通用的多条件组合查询，包括图片，分类名称
     */
    @Select({"<script>",
            "select * from goods",
            "<where>",
            "<if test= 'name!= null and name.length>0'>name like '%${name}%'</if>",
            "<if test= 'recom!= null'>and recom=#{recom},</if>",
            "<if test= 'categoryId!= null'>and category_id=#{categoryId},</if>",
            "<if test= 'status!= null'>and status=#{status},</if>",
            "</where>",
            "</script>"
    })
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "category_id",property = "categoryId"),
            @Result(column = "id", property = "pics",
                    many = @Many(select = "com.example.stmall.manager.mapper.GoodsPicMapper.selectByGoodsId")),
            @Result(column = "category_id",property = "category",
                one=@One(select = "com.example.stmall.manager.mapper.CategoryMapper.selectById"))
    })
    List<Goods> select(Goods goods);
}
